package com.jzl.swing.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.Date;
import java.util.Locale;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.eltima.components.ui.DatePicker;
import com.jzl.swing.entity.Bill;
import com.jzl.swing.service.BillService;
import com.jzl.swing.service.impl.BillServiceImpl;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.awt.event.ActionEvent;

public class ModifyBillUI {

	private JFrame frame;
	private JTextField textFieldProductName;
	private JTextField textFieldProductNumber;
	private JTextField textField_2;
	private JTextField textFieldPrice;
	private String bill_number = "1";
	private Bill theBill;
	BillService billService = new BillServiceImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ModifyBillUI window = new ModifyBillUI(args[0]);
//					ModifyBillUI window = new ModifyBillUI();
					
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ModifyBillUI(String bill_number) {
		this.bill_number = bill_number;
		try {
			theBill = billService.queryByNumber(bill_number);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initialize();
	}
	
	public ModifyBillUI() {
		try {
			theBill = billService.queryByNumber(this.bill_number);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 800);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		datepick.setBounds(160, 563, 200, 50);
		frame.getContentPane().add(datepick);
		
		JLabel label = new JLabel("商品名称：");
		label.setBounds(43, 35, 75, 24);
		frame.getContentPane().add(label);
		
		textFieldProductName = new JTextField();
		textFieldProductName.setBounds(137, 34, 130, 26);
		frame.getContentPane().add(textFieldProductName);
		textFieldProductName.setColumns(10);
		textFieldProductName.setText(theBill.getBill_product_name());
		
		JLabel label_1 = new JLabel("交易数量：");
		label_1.setBounds(43, 98, 75, 16);
		frame.getContentPane().add(label_1);
		
		textFieldProductNumber = new JTextField();
		textFieldProductNumber.setBounds(137, 93, 130, 26);
		frame.getContentPane().add(textFieldProductNumber);
		textFieldProductNumber.setColumns(10);
		textFieldProductNumber.setText(theBill.getBill_product_number());
		
		JLabel label_2 = new JLabel("交易单位：");
		label_2.setBounds(43, 165, 75, 16);
		frame.getContentPane().add(label_2);
		
		textField_2 = new JTextField();
		textField_2.setBounds(137, 160, 130, 26);
		frame.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		textField_2.setText("元");
		
		JLabel label_3 = new JLabel("交易金额：");
		label_3.setBounds(43, 226, 75, 16);
		frame.getContentPane().add(label_3);
		
		textFieldPrice = new JTextField();
		textFieldPrice.setBounds(137, 221, 130, 26);
		frame.getContentPane().add(textFieldPrice);
		textFieldPrice.setColumns(10);
		textFieldPrice.setText(theBill.getBill_price());
		
		JLabel label_4 = new JLabel("是否付款：");
		label_4.setBounds(43, 286, 75, 16);
		frame.getContentPane().add(label_4);
		
		JComboBox<String> comboBoxIsPaid = new JComboBox<String>();
		comboBoxIsPaid.setModel(new DefaultComboBoxModel<String>(new String[] {"是", "否"}));
		comboBoxIsPaid.setBounds(137, 282, 67, 27);
		frame.getContentPane().add(comboBoxIsPaid);
		if (theBill.getBill_ispaid().equals("是")) {
			comboBoxIsPaid.setSelectedIndex(0);
		}else {
			comboBoxIsPaid.setSelectedIndex(1);
		}
		
		JLabel label_5 = new JLabel("所属供应商：");
		label_5.setBounds(43, 346, 89, 16);
		frame.getContentPane().add(label_5);
		
		JComboBox<String> comboBoxSupplier = new JComboBox<String>();
		comboBoxSupplier.setModel(new DefaultComboBoxModel<String>(new String[] {"阿里巴巴", "腾讯科技", "网易", "百度"}));
		comboBoxSupplier.setBounds(144, 342, 124, 27);
		frame.getContentPane().add(comboBoxSupplier);
		
		JLabel label_6 = new JLabel("商品描述：");
		label_6.setBounds(43, 408, 75, 16);
		frame.getContentPane().add(label_6);
		
		JTextArea textAreaDesc = new JTextArea();
		textAreaDesc.setBounds(144, 396, 184, 104);
		frame.getContentPane().add(textAreaDesc);
		textAreaDesc.setText(theBill.getBill_product_decrib());
		
		JLabel lblNewLabel = new JLabel("交易时间：");
		lblNewLabel.setBounds(43, 563, 75, 16);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblYyyy = new JLabel("yyyy-MM-dd");
		lblYyyy.setBounds(43, 614, 89, 26);
		frame.getContentPane().add(lblYyyy);
		lblYyyy.setText(theBill.getBill_date());
		
		JButton button = new JButton("获取时间");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String simDate = simpleDateFormat.format(datepick.getValue());
                System.out.println(simDate);//这是一个java.util.Date对象
                lblYyyy.setText(simDate);
                JOptionPane.showMessageDialog(frame, "获取控件中的日期：" + lblYyyy.getText());
			}
		});
		button.setBounds(151, 614, 117, 29);
		frame.getContentPane().add(button);
		
		JButton button_1 = new JButton("修改");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if ("".equals(textFieldProductName.getText())) {
					JOptionPane.showMessageDialog(frame, "商品名不能为空");
					return;
				}
				if ("".equals(textFieldProductNumber.getText())) {
					JOptionPane.showMessageDialog(frame, "商品数量不能为空");
					return;
				}
				if ("".equals(textFieldPrice.getText())) {
					JOptionPane.showMessageDialog(frame, "商品价格不能为空");
					return;
				}
				DecimalFormat df = new DecimalFormat("#.00");
				try {
					textFieldPrice.setText(String.valueOf(df.format(Double.parseDouble(textFieldPrice.getText()))));;
					
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(frame, "商品价格格式错误");
					return;
				}
				
				
				theBill.setBill_product_name(textFieldProductName.getText());
				theBill.setBill_product_number(textFieldProductNumber.getText());
				theBill.setBill_price(textFieldPrice.getText());
				theBill.setBill_product_decrib(textAreaDesc.getText());
				theBill.setBill_supplierName(comboBoxSupplier.getSelectedItem().toString());
				theBill.setBill_ispaid(comboBoxIsPaid.getSelectedItem().toString());
				theBill.setBill_date(lblYyyy.getText());
				try {
					billService.modifyBill(theBill);
					JOptionPane.showMessageDialog(frame, "修改成功！");
					frame.dispose();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		button_1.setBounds(42, 740, 117, 29);
		frame.getContentPane().add(button_1);
		
		JButton button_2 = new JButton("关闭");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(frame, "是否确定关闭？您所做的修改将不会被保存");
				if (i==0) {
					frame.dispose();
				}
			}
		});
		button_2.setBounds(237, 740, 117, 29);
		frame.getContentPane().add(button_2);
		
		JLabel label_7 = new JLabel("*");
		label_7.setForeground(Color.RED);
		label_7.setBounds(33, 39, 11, 16);
		frame.getContentPane().add(label_7);
		
		JLabel label_8 = new JLabel("*");
		label_8.setForeground(Color.RED);
		label_8.setBounds(33, 98, 11, 16);
		frame.getContentPane().add(label_8);
		
		JLabel label_9 = new JLabel("*");
		label_9.setForeground(Color.RED);
		label_9.setBounds(33, 165, 11, 16);
		frame.getContentPane().add(label_9);
		
		JLabel label_10 = new JLabel("*");
		label_10.setForeground(Color.RED);
		label_10.setBounds(33, 226, 11, 16);
		frame.getContentPane().add(label_10);
		
		JLabel label_11 = new JLabel("*");
		label_11.setForeground(Color.RED);
		label_11.setBounds(33, 286, 11, 16);
		frame.getContentPane().add(label_11);
		
		JLabel label_12 = new JLabel("*");
		label_12.setForeground(Color.RED);
		label_12.setBounds(33, 346, 11, 16);
		frame.getContentPane().add(label_12);
		
		JLabel label_13 = new JLabel("*");
		label_13.setForeground(Color.RED);
		label_13.setBounds(33, 563, 11, 16);
		frame.getContentPane().add(label_13);
		
		
		
	}
	
	
	
	final DatePicker datepick = getDatePicker();
	
    private DatePicker getDatePicker() {
        final DatePicker datepick;
        // 格式
        String DefaultFormat = "yyyy-MM-dd HH:mm:ss";
        // 当前时间
        Date date = new Date();
        // 字体
        Font font = new Font("Times New Roman", Font.BOLD, 14);

        Dimension dimension = new Dimension(177, 24);

        int[] hilightDays = { 1, 3, 5, 7 };

        int[] disabledDays = { 4, 6, 5, 9 };
    //构造方法（初始时间，时间显示格式，字体，控件大小）
        datepick = new DatePicker(date, DefaultFormat, font, dimension);

        datepick.setLocation(137, 83);//设置起始位置
        /*
        //也可用setBounds()直接设置大小与位置
        datepick.setBounds(137, 83, 177, 24);
        */
        // 设置一个月份中需要高亮显示的日子
        datepick.setHightlightdays(hilightDays, Color.red);
        // 设置一个月份中不需要的日子，呈灰色显示
        datepick.setDisableddays(disabledDays);
        // 设置国家
        datepick.setLocale(Locale.CANADA);
        // 设置时钟面板可见
        datepick.setTimePanleVisible(true);
        return datepick;
    }

}
